﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace FA.FlightAttendant.View.Extensions
{
    /// <summary>
    /// Allows binding to the DialogResult of a window in the view model such that
    /// a dialog can be submitted without having to violate the view/viewmodel
    /// relationship.
    /// </summary>
    public class DialogCloser : DependencyObject
    {
        /// <summary>
        /// DialogResult DependencyProperty.
        /// </summary>
        public static readonly DependencyProperty DialogResultProperty =
            DependencyProperty.RegisterAttached(
                "DialogResult",
                typeof(bool?),
                typeof(DialogCloser),
                new PropertyMetadata(DialogResultChanged));

        private static void DialogResultChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var window = d as Window;
            if (window != null)
                window.DialogResult = e.NewValue as bool?;
        }

        public static void SetDialogResult(Window target, bool? value)
        {
            target.SetValue(DialogResultProperty, value);
        }
    }
}
